// pages/distributor/distributor.js
var app = getApp();
var utils = require('../../utils/shop/formatShop.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disArgs: {
      act: 'get_dis_list',
      User_ID: 0,
      level: 0, //分销商等级,默认为0
      page: 1,
      pageSize: 10
    },

    userlist: [],

    //是否能获取数据状态  
    get_flag: true,
    //加载更多动画
    loading: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //隐藏加载框
    wx.hideLoading();
    if (options.disid) {
      //获取分销商级别
      this.setData({ 'disArgs.level': options.disid })
    }
    if (options.level_name) {
      app.setNavBarTitle('我的' + options.level_name);
    }

    this.load();
  },

  //页面加载
  load: function () {
    //检查用户是否登录
    if (!app.globalData.userInfo) {
      app.login('', this.load);
      return false;
    }

    this.setData({
      'disArgs.User_ID': app.globalData.userInfo.User_ID
    });

    //获取分销商列表
    this.getUserList();
  },

  //分销商列表处理
  getUserList: function (res) {
    this.setData({
      loading: true
    });
    let that = this;
    app.http_req(this.data.disArgs, app.globalData.init.api_url, 'POST', function (res) {
      if (res.errorCode === 0) {
        let lists = res.data;
        for (var i in lists) {
          lists[i].Account_CreateTime = utils.formatDate(lists[i]['Account_CreateTime']);
          lists[i].User_NickName = lists[i]['User_NickName'] ? lists[i]['User_NickName'] : '暂无昵称';
          lists[i].User_HeadImg = lists[i]['User_HeadImg'] ? (lists[i]['User_HeadImg'].indexOf('http') > -1 ? lists[i]['User_HeadImg'] : app.globalData.init.front_url + lists[i]['User_HeadImg']) : '../../images/moren.png';
        }
        that.setData({
          userlist: that.data.userlist.concat(lists),
          'disArgs.page': parseInt(that.data.disArgs.page) + 1,
          get_flag: Math.ceil(res.totalCount / that.data.disArgs.pageSize) > that.data.disArgs.page ? true : false
        })
      } else if (res.errorCode == 2) {
        that.setData({
          get_flag: false
        });
      }

      that.setData({
        loading: false
      });

      // 结束下拉刷新
      utils.overPullDownRefresh();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //显示加载导航条
    wx.showNavigationBarLoading();
    //初始化数据 (防止数据重复)
    this.setData({
      'disArgs.page': 1,
      userlist: [],
      //是否能获取数据状态  
      get_flag: true,
      //加载更多动画
      loading: true,
    });
    this.load();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.get_flag && !this.data.loading) {
      this.getUserList();
    }
  }
})